﻿namespace Hotel.Web.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using ManagementModule = Hotel.Module.Management;
    using ResourcesModule = Hotel.Module.Resources;

    public class Assignment
    {
        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "WorkerLogin")]
        public string WorkerLogin { get; set; }

        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "AssignmentNumber")]
        public int ID { get; set; }

        [Required(ErrorMessageResourceType = typeof(Hotel.Web.Views.Shared.Validation), ErrorMessageResourceName = "RequiredErrorMessage")]
        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "RoomNumber")]
        [Range(1, 72, ErrorMessageResourceType = typeof(Hotel.Web.Views.Shared.Validation), ErrorMessageResourceName = "RoomNumberErrorMessage")]
        public int RoomNumber { get; set; }

        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "Service")]
        public Service CurrentService { get; set; }
       
        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "Description")]
        [DisplayFormat(ConvertEmptyStringToNull = false)]
        public string Description { get; set; }

        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "AssignmentState")]
        public CurrAssignmentState AssignmentState
        {
            get { return currAssignmentState; }
            set { currAssignmentState = value; }
        }

        public enum CurrAssignmentState
        {
            Open,
            Aplied,
            Finished,
        }

        [Display(ResourceType = typeof(Hotel.Web.Views.Assignment.AssignmentResources), Name = "WorkerType")]
        public CurrentWorkerType WorkerType
        {
            get { return currWorkerType; }
            set { currWorkerType = value; }
        }

        public enum CurrentWorkerType
        {
            Cleaners, Repairman
        }
        
        public static Assignment FromDomainModel(ManagementModule.Models.Assignment ass)
        {
            Hotel.Web.Models.Assignment ret = new Assignment();
            ret.WorkerLogin = ass.WorkerLogin;
            ret.WorkerType = (CurrentWorkerType)ass.WorkerType;
            ret.ID = ass.ID;
            ret.RoomNumber = ass.RoomNumber;
            ret.Description = ass.Description;
            using (ResourcesModule.HotelResourcesController m_ResourcesController = Helpers.ServiceLocator.Instance.GetService(typeof(ResourcesModule.IResourseManagment)) as ResourcesModule.HotelResourcesController)
            {
                Hotel.Module.Resources.Models.Service s = m_ResourcesController.GetServiceByID(ass.ServiceID);
                ret.CurrentService = Service.FromDomainModel(s);
            }
            ret.AssignmentState = (CurrAssignmentState)ass.State;
            return ret;
        }

        public void AssignTo(object target)
        {
            if (target is Hotel.Module.Management.Models.Assignment)
            {
                Hotel.Module.Management.Models.Assignment modelAssignment = target as Hotel.Module.Management.Models.Assignment;
                
                modelAssignment.WorkerLogin = WorkerLogin;
                modelAssignment.WorkerType = (Hotel.Module.Management.Models.Assignment.CurrentWorkerType)WorkerType;
                modelAssignment.RoomNumber = RoomNumber;
                modelAssignment.Description = Description;
                modelAssignment.ID = ID;
                using (ResourcesModule.HotelResourcesController m_ResourcesController = Helpers.ServiceLocator.Instance.GetService(typeof(ResourcesModule.IResourseManagment)) as ResourcesModule.HotelResourcesController)
                {
                    modelAssignment.ServiceID = m_ResourcesController.GetServiceByName(this.CurrentService.Name).ID;
                }
                return;
            }

            throw new NotSupportedException();
        }

        private CurrAssignmentState currAssignmentState;
        private CurrentWorkerType currWorkerType;
 }
}